//
//  LocalMusicTableViewController.m
//  MoeFM
//


//  播放ipod媒体库音乐,因为音频引擎不支持,所以砍掉了,可以用avaudioplayer播放

//  Created by miyang on 14-11-6.
//  Copyright (c) 2014年 StraightJet. All rights reserved.
//

#import "LocalMusicTableViewController.h"

@interface LocalMusicTableViewController ()<MPMediaPickerControllerDelegate>

@end

@implementation LocalMusicTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        _nameArr = [[NSMutableArray alloc] init];
        _itemArr = [[NSArray alloc] init];
        NSArray *pathsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [pathsArr objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"collection.data"];
        
        _itemArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        if (_itemArr != 0) {
            for (MPMediaItem *item in _itemArr) {
                NSLog(@"%@",[item valueForProperty:MPMediaItemPropertyTitle]);
                [_nameArr addObject:item];
            }
        }
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(120, 0, 80, 40);
   // btn.backgroundColor = [UIColor blackColor];
    [btn addTarget:self action:@selector(pick) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"选择歌曲" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    
    
    [head addSubview:btn];
    self.tableView.tableHeaderView = head;
    
}

- (void)pick
{
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
    picker.prompt = @"请选择需要播放的歌曲";
    picker.showsCloudItems = NO;
    picker.allowsPickingMultipleItems = YES;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - MPMediaPickerControllerDelegate

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
    [mediaPicker dismissViewControllerAnimated:YES completion:nil];
}

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    [mediaPicker dismissViewControllerAnimated:YES completion:nil];
    _itemArr = mediaItemCollection.items;
    NSArray *pathsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [pathsArr objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"collection.data"];
    BOOL flag = [NSKeyedArchiver archiveRootObject:_itemArr toFile:path];
    if (flag) {
        NSLog(@"success");
    }
    
    [_nameArr removeAllObjects];
    for (MPMediaItem *item in _itemArr) {
        NSLog(@"%@",[item valueForProperty:MPMediaItemPropertyTitle]);
        [_nameArr addObject:item];
    }
    [self.tableView reloadData];
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_nameArr.count == 0) {
        return 10;
    }
    return _nameArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collection"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"collection"];
    }
    
    if (_nameArr.count != 0) {
        cell.textLabel.text = [[_nameArr objectAtIndex:indexPath.row] valueForProperty:MPMediaItemPropertyTitle];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.2f];
    if (_nameArr.count == 0) {
        return;
    }
    
//    [[PlayerViewController sharPlayer] playingWithIpodItems:_itemArr andIndex:indexPath.row];
    
}

- (void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

@end
